/**
 * Created by admin on 2017/8/23.
 */
layui.use(['element','form','global','laydate','jquery','collage','major','userInfo','pageFlip'], function(){
    var element = layui.element;
    var form = layui.form;
    var global = layui.global;
    var collage = layui.collage;
    var major = layui.major;
    var userInfo = layui.userInfo;
    var pageFlip = layui.pageFlip;
    var laydate = layui.laydate;
    var $ = layui.jquery;

    //执行一个laydate实例
    laydate.render({
        elem: '#birth' //指定元素
    });
    var init = {
        findCollageList: function () {
            collage.findCollageList(null, function (resp) {
                if (resp.c == "100") {
                    var temp = '<option  value="">请选择学院</option>';
                    for (var i = 0; i < resp.d.l.length; i++) {
                        temp += '<option value="' + resp.d.l[i].collageId + '" >' + resp.d.l[i].collageName + '</option>';
                    }
                    // $('#collageId').html(temp);
                    $('#searchForm select[name="collageId"]').html(temp);
                    $('#addUserForm select[name="collageId"]').html(temp);
                    form.render('select');
                } else {
                    layer.msg(resp.m);
                }
            })
        },
        findMajorList: function (data,element) {
            var obj = {
                collageId:data
            }
            major.findMajorList(obj, function (resp) {
                if (resp.c == "100") {
                    var temp = '<option  value="">请选择专业</option>';
                    for (var i = 0; i < resp.d.l.length; i++) {
                        temp += '<option value="' + resp.d.l[i].majorId + '" >' + resp.d.l[i].majorName + '</option>';
                    }
                    $('#'+element+ ' select[name="majorId"]').html(temp);
                    form.render('select');
                } else {
                    layer.msg(resp.m);
                }
            })
        },
        findUserInfoList: function () {
            var param = global.form.serializeJSON('#searchForm');
            param.currentPage=1;
            pageFlip.configure(param, userInfo.findUserInfoList, refreshTable, 'userPageCtrl');
        }
    }
    init.findUserInfoList();
    init.findCollageList();
    init.findMajorList(0,'searchForm');
    init.findMajorList(0,'addUserForm');
    //搜索
    $("#btnQuery").click(function () {
        init.findUserInfoList();
    });
    function refreshTable(data, page) {
        var lines = [];
        for (var i = 0; i < data.length; i++) {
            var userInfo = data[i];
            var line = '<tr>';
            line += '<td>' + userInfo.name + '</td>';
            line += '<td>' + (userInfo.sex==0?"男":"女") + '</td>';
            line += '<td>' + userInfo.birth + '</td>';
            line += '<td>' + userInfo.phone + '</td>';
            line += '<td>' + userInfo.collageName + '</td>';
            line += '<td>' + userInfo.majorName + '</td>';
            line += '<td>' + userInfo.grade + '</td>';
            line += '<td>' + userInfo.qq + '</td>';
            line += '<td>' + userInfo.wechat + '</td>';
            line += '<td>' + userInfo.mail + '</td>';
            line += '<td>' + userInfo.city + '</td>';
            line += '<td>' +
                // '<button class="layui-btn layui-btn-normal edit" name="' + userInfo.id + '">编辑</button>' +
                '<button class="layui-btn btn-disabled" name="' + userInfo.id + '">编辑</button>' +
                '<button class="layui-btn btn-disabled" name="' + userInfo.id + '">删除</button>' +
                // '<button class="layui-btn layui-btn-normal del" name="' + couponItem.id + '">删除</button>' +
                '</td>';
            line += '</tr>';
            lines.push(line);
        }
        $('#userTable tbody').empty().append(lines.join());
    }

    form.on('select(collageId)', function (data) {
        init.findMajorList(data.value,'searchForm');
    });

    form.on('select(collageId1)', function (data) {
        init.findMajorList(data.value,'addUserForm');
    });

    $("#addUser").click(function () {
        $("#addUserForm").trigger("reset");
        layer.open({
            type: 1,
            title: "新增信息",
            area: ['700px', '550px'],
            fix: false, //不固定
            maxmin: true,
            content: $("#userForm"),
            zIndex:1001,
            btn: ["保存", "取消"],
            yes: function () {
                var param = global.form.serializeJSON($('#addUserForm'));
                userInfo.saveUserInfo(param, function (resp) {
                    if (resp.c == "100") {
                        layer.alert("新增成功，请注意查收短信和邮件，谢谢！",function(){
                            layer.closeAll();
                            init.findUserInfoList();
                        });
                    } else {
                        layer.msg(resp.m);
                    }
                })
            },
            btn2: function () {
                layer.msg("取消了");
                layer.closeAll();
            }
        });
    });
});